var table = null;
layui.use(['table', 'form', 'element', 'layer'], function () {
    table = layui.table;
    var form = layui.form;
    var element = layui.element;
    var layer = layui.layer;

    /*初始化数据表格*/
    table.render({
        elem: '#dataGrid'
        , url: '/admin/sysRole/list'
        , method: 'post'
        , page: true
        , even: true //开启隔行背景
        // , size: 'sm'
        , cols: [[
            {type: 'numbers', fixed: "left"}
            , {type: 'checkbox', fixed: "left"}
            , {field: 'roleName', title: '角色名', width: 100}
            , {field: 'note', title: '描述', width: 180}
            , {field: 'status', title: '状态', width: 85, templet: '#switch', unresize: true}
            , {
                field: 'createTime', title: '创建时间', width: 180, templet: function (d) {
                    if (d.createTime != null) {
                        return new Date(d.createTime).format('yyyy-MM-dd hh:mm:ss')
                    }
                }
            }
            , {field: 'createUserId', title: '创建人', width: 180}
            , {fixed: 'right', width: 300, toolbar: '#bars'}
        ]]
    });

    //监听查询
    form.on('submit(search)', function (data) {
        table.reload('dataGrid', {
            where: data.field
        });
        return false;
    });

    //监听提交
    form.on('submit(save)', function (data) {
        layer.confirm('确定提交吗?', {icon: 3, title: '提示', offset: '150px'}, function (index) {
            var checknodes = $("#menu-tree").tree('getChecked');
            var innodes = $("#menu-tree").tree('getChecked', 'indeterminate');
            buildTreeData(checknodes);
            buildTreeData(innodes);
            $("#editForm").ajaxSubmit({
                url: "/admin/sysRole/save",
                type: 'POST',
                success: function (result) {
                    if (result.success == true) {
                        layer.close(index);
                        layer.msg(result.msg, {
                            icon: 6,
                            offset: '150px',
                            time: 1000
                        }, function () {
                            window.location.href = '/admin/sysRole/index'
                        });
                    } else {
                        layer.msg(result.msg, {
                            icon: 5,
                            offset: '150px'
                        });
                    }
                },
            });
        });
        return false;
    });

    /*监听开关*/
    form.on('switch(status)', function (obj) {
        var status = obj.elem.checked ? '1' : '0';
        $.ajax({
            type: "post",
            url: "/admin/sysRole/updateStatus",
            data: {'id': obj.value,"status":status},
            dataType: "json",
            success: function (result) {
                if (result.success == true) {
                    layer.msg(result.msg, {
                        icon: 6,
                        offset: '150px',
                        time: 300
                    }, function () {
                        table.reload('dataGrid', {
                            where: $('#searchForm').serializeJSON()
                        });
                    });
                } else {
                    layer.msg(result.msg, {
                        icon: 5,
                        offset: '150px'
                    });
                }
            }
        });
        // layer.tips(this.value, obj.othis);
    });

    /*绑定工具栏*/
    var $ = layui.$, active = {
        barTableAdd: function () { //获取选中数据
            window.location.href = '/admin/sysRole/add'
        }
        , barTableEdit: function () { //获取选中数目
            var checkStatus = table.checkStatus('dataGrid');
            if (checkStatus.data.length == 0) {
                layer.msg("未选中数据", {
                    icon: 5,
                    offset: '150px'
                });
            } else if (checkStatus.data.length > 1) {
                layer.msg("只能勾选一条数据", {
                    icon: 5,
                    offset: '150px'
                });
            } else {
                window.location.href = '/admin/sysRole/add?id=' + checkStatus.data[0].id
            }
        }
        , barTableRemove: function () {
            var checkStatus = table.checkStatus('dataGrid');
            if (checkStatus.data.length == 0) {
                layer.msg("未选中数据", {
                    icon: 5,
                    offset: '150px'
                });
            } else {
                layer.confirm('确定删除吗?', {icon: 3, title: '提示', offset: '150px'}, function (index) {
                    var ids = new Array();
                    for (index in checkStatus.data) {
                        var id = checkStatus.data[index].id;
                        ids.push(id);
                    }
                    $.ajax({
                        type: "post",
                        url: "/admin/sysRole/delete",
                        data: {'ids': ids},
                        dataType: "json",
                        success: function (result) {
                            if (result.success == true) {
                                layer.close(index);
                                layer.msg(result.msg, {
                                    icon: 6,
                                    offset: '150px',
                                    time: 1000
                                }, function () {
                                    table.reload('dataGrid', {
                                        where: $('#searchForm').serializeJSON()
                                    });
                                });
                            } else {
                                layer.msg(result.msg, {
                                    icon: 5,
                                    offset: '150px'
                                });
                            }
                        }
                    });
                });
            }
        }
    };

    $('.barTable .layui-btn').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
});
var buildTreeData = function (nodes) {
    $.each(nodes, function (i, note) {
        var id = note.attributes.id;
        var type = note.attributes.type;
        var $id = $("<input type='hidden' name='menuIds' class='c_menus'>");
        if (type == 0) {
            $id.attr('name', 'menuIds');
        } else if (type == 1) {
            $id.attr('name', 'btnIds');
        }
        $id.val(id);
        $("#editForm").append($id);
    });
}






